<?php
 /**
  * Feedback模型类(数据库模型)
  *   
  * @author wenqiang@6rooms.com
  * @version $Id: Feedback.class.php,v 1.0 2013/12/3 13:36:47 gaowenqiang Exp $   
  * @package sr.model
  *
  */
class Sr_Model_Feedback extends Sr_Model_Base
{
	// 按记录创建(本地时间)查询
	const QUERY_LIST_COLUMN_TM = 1;
	// 按feedback时间查询
	const QUERY_LIST_COLUMN_RTM = 2;

	/**
	 * 表名
	 * @var string
	 *
	 */
	protected $name = 'feedback';

	/**
	 * 主键名
	 * @var string
	 *
	 */
	protected $primary = 'id';
	
	/**
	 * 构造器
	 * @var array
	 *
	 */
	public function __construct($args = null)
	{
		parent::__construct($args);
	}
	
	/*
	 * 获取查询条件
	 * @param array $cond 查询条件
 	 *  prod int 产品id
	 *	start int 超始时间戳
	 *  end int 结束时间戳
	 *  token string 设备标识
	 * @return int
	 *
	 */
	protected function where($cond) 
	{
		$where = array();
		if (isset($cond['start']) && isset($cond['end']) && 
				strlen($cond['start']) && strlen($cond['end']) && 
				isset($cond['timeColumn']) && strlen($cond['timeColumn'])) {
			$where[] = "{$cond['timeColumn']} >= {$cond['start']} AND {$cond['timeColumn']} <= " . (is_string($cond['end']) ? "'{$cond['end']}'" : $cond['end']);
		}
		
		if (isset($cond['prod']) && !empty($cond['prod'])) {
			$where[] = "prod = {$cond['prod']}";
		}

		if (isset($cond['token']) && !empty($cond['toen'])) {
			$where[] = "token = '{$token}'";
		}

		return count($where) ? 'WHERE ' . implode(' AND ', $where) : '';
	}

	/**
	 *
	 * 获取feedback数据
	 * 参数说明: [<prod> && <type> && <tms>] || <token>
	 * @param $cond array 查询条件
	 *  prod int 产品编号
	 *  type int 1/按本地时间查询 2/按feedback接口返回时间查询
	 *  tms int 时间戳(起始时间)
	 *  token string 设备唯一标识
	 * @return array
	 *
	 */
	public function getList($param) {
		$cond = array();
		if (strlen($param['token'])) {
			$cond['token'] = $param['token'];
			$cond['orderColumn'] = 'tm';
		} else {
			$cond['prod'] = $param['prod'];

			if ((int)$param['type'] === self::QUERY_LIST_COLUMN_TM) {
				$cond['timeColumn'] = 'tm';
				$cond['start'] = "FROM_UNIXTIME({$param['tms']})";
				$cond['end'] = date('Y-m-d H:i:s');
			} else {
				$cond['timeColumn'] = 'rtm';
				$cond['start'] = $param['tms'];
				$cond['end'] = time();
			}
			$cond['orderColumn'] = $cond['timeColumn'];

		}
		$column = 'rtm, tkl, token, tm';
		$where = $this->where($cond);
		$order = "ORDER BY {$cond['orderColumn']} DESC";
		$sql = "SELECT {$column} FROM {$this->name} {$where} {$order}";

		return $this->fetchAll($sql);
	}
	
	/**
	 *
	 * 数据整理
	 * @param array $rows 
	 * @return array
	 *
	 */
	public function format($rows) {
		$data = array();
		if (!empty($rows)) {
			foreach($rows as $key => $row) {
				if (is_array($row) && !empty($rows)) {
					$tmp = array();
					foreach ($row as $name => $value) {
						$fieldKey = null;
						$fieldVal = $value;
						switch ($name) {
							case 'rtm':
								$fieldKey = 'timestamp';
								break;
							case 'tkl':
								$fieldKey = 'tokenLength';
								break;
							case 'token':
								$fieldKey = 'deviceToken';
								break;
							case 'tm' :
								$fieldKey = 'tm';
								$fieldVal = strtotime($value);
								break;
							default:
								$fieldKey = $name;
								break;
						}
						$tmp[$fieldKey] = is_numeric($fieldVal) ? $fieldVal + 0 : $fieldVal;
					}
					$data[] = $tmp;
				}
			}
		}

		return $data;
	}
}